package com.fm.financemanage.controller;


import com.fm.financemanage.component.TokenComponent;
import com.fm.financemanage.constant.PlatTypeEnum;
import com.fm.financemanage.constant.ResponseEnum;
import com.fm.financemanage.constant.annotation.Require;
import com.fm.financemanage.exception.ParameterException;
import com.fm.financemanage.model.po.CustomerCompanyAccountConfig;
import com.fm.financemanage.model.po.CustomerCompanyInfo;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.company.CompanyAccountConfigOperatorVo;
import com.fm.financemanage.model.vo.company.CustomerCompanyInfoListVo;
import com.fm.financemanage.model.vo.company.CustomerCompanyOperatorVo;
import com.fm.financemanage.model.vo.equipment.BusinessEquipmentListVo;
import com.fm.financemanage.model.vo.equipment.BusinessEquipmentOperatorVo;
import com.fm.financemanage.response.Callback;
import com.fm.financemanage.response.JsonData;
import com.fm.financemanage.service.CustomerCompanyInfoService;
import com.fm.financemanage.service.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/customer/company/info")
@Api(tags = "客户信息管理")
@RestController
@Validated
public class CustomerCompanyInfoController {


    @Autowired
    private CustomerCompanyInfoService customerCompanyInfoService;

    @Autowired
    private TokenComponent tokenComponent;

    @Require
    @GetMapping("/")
    @ApiOperation("查询列表")
    public JsonData<PageVo<List<CustomerCompanyInfoListVo>>> getList(HttpServletRequest request, HttpServletResponse response,
                                                                     @ApiParam(name = "accountantId", value = "会计员Id", required = true)
                                                                     @RequestParam(name = "accountantId", required = false) Integer accountantId,
                                                                     @ApiParam(name = "companyClassifyId", value = "公司分类id", required = true)
                                                                     @RequestParam(name = "companyClassifyId", required = false) Integer companyClassifyId,
                                                                     @ApiParam(name = "name", value = "公司名称", required = true)
                                                                     @RequestParam(name = "name", required = false) String name,
                                                                     @ApiParam(name = "page", value = "页码", required = true)
                                                                     @RequestParam(name = "page", required = false) Integer page,
                                                                     @ApiParam(name = "limit", value = "条数", required = true)
                                                                     @RequestParam(name = "limit", required = false) Integer limit
    ) {
        final String uuid = request.getAttribute("uuid").toString();


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), customerCompanyInfoService.getList(uuid, null, companyClassifyId,
                accountantId, name, page, limit));
    }

    @Require
    @PostMapping("/")
    @ApiOperation("")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @RequestBody CustomerCompanyOperatorVo customerCompanyOperatorVo, BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            throw new ParameterException(bindingResult.getAllErrors());
        }
        final String uuid = request.getAttribute("uuid").toString();
        customerCompanyOperatorVo.setUserUuid(uuid);
        customerCompanyInfoService.operator(customerCompanyOperatorVo);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

    @Require
    @DeleteMapping("/{uuid}")
    @ApiOperation("删除")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("uuid") String uuid
    ) {

        customerCompanyInfoService.removeById(uuid);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

    @Require
    @PostMapping("/account")
    @ApiOperation("新增修改公司平台账号")
    public JsonData operatorAccount(HttpServletRequest request, HttpServletResponse response,
                                    @RequestBody CompanyAccountConfigOperatorVo CompanyAccountConfigOperatorVo, BindingResult bindingResult
    ) {

        if (bindingResult.hasErrors()) {
            throw new ParameterException(bindingResult.getAllErrors());
        }
        PlatTypeEnum p = PlatTypeEnum.getByType(CompanyAccountConfigOperatorVo.getType());
        if (p == null) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), "平台类型错误");
        }
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), customerCompanyInfoService.operatorAccount(CompanyAccountConfigOperatorVo));
    }
    @Require
    @DeleteMapping("/account/{uuid}")
    @ApiOperation("删除公司账号")
    public JsonData removeByAccountId(HttpServletRequest request, HttpServletResponse response,
                                      @PathVariable("uuid") String uuid
    ) {

        customerCompanyInfoService.removeByAccountId(uuid);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

}
